local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local timetool = require "timetool"
local tabletool = require"tabletool"
require "fishenum"
local tablelogic 	-- = require "tablelogic"
local seatlogic 	-- = require "seatlogic"
local gamelogic 	-- = require "gamelogic"

local server, table_data
local TableRequest = {}

function TableRequest.init(base)
    server = base
    table_data = server.table_data
	tablelogic = server.logic.tablelogic
	seatlogic = server.logic.seatlogic
	gamelogic = server.logic.gamelogic
end

function TableRequest.disconnect(rid, rdata, request)
	filelog.debug("TableRequest.disconnect:", request)
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		return false
	end
	tablelogic.disconnect(table_data, seat)
    return true
end

function TableRequest.entertable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器维护中"
		return responsemsg
	end

	-- if msghelper:callmsg_to_gatesvr("check_role_state", rid, 0) then
	-- 	responsemsg.retcode = EErrCode.ERR_HAD_IN_TABLE
	-- 	responsemsg.retstr = "已经在游戏中了！"
	-- 	filelog.debug("entertable: 已经在游戏中了！")
	-- 	return responsemsg
	-- end

	--判断玩家金币是否够最小携带
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
		if not rdata.money.diamond or rdata.money.diamond < table_data.conf.min_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您携带的钱不足，请前往充值！"
			return responsemsg
		end
	end

	if tablelogic.is_full(table_data) then
		responsemsg.retcode = EErrCode.ERR_TABLE_FULL
		responsemsg.retstr = "房间人数已满！"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)

	tablelogic.entertable(table_data, rid, rdata, request, seat)
	
	if rdata.info.level == 99 then
		TableRequest.sitdowntable(rid, rdata, request)
	end
    return responsemsg
end

function TableRequest.reentertable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
    local waitinfo = table_data.waits[rid]
	if seat then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
		if seat.is_tuoguan == EBOOL.TRUE then
			seat.is_tuoguan = EBOOL.FALSE
			seat.is_disconnect = nil
		end
	elseif waitinfo then
		waitinfo.playerinfo.rolename = rdata.info.rolename
		waitinfo.playerinfo.logo = rdata.info.logo
		waitinfo.playerinfo.sex = rdata.info.sex		
	end

	if waitinfo == nil and seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效的请求！"
		return responsemsg
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	
	if seat then
		msghelper:sendmsg_to_tableplayer(seat, "reentertableR", responsemsg)
		tablelogic.reentertable(table_data, request, seat)	 
	end
	-- return responsemsg
end

function TableRequest.leavetable(rid, rdata, request)
	local responsemsg = {}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
		if seat == nil then
			tablelogic.leavetable(table_data, rid)
			return responsemsg
		end
	end
	tablelogic.standuptable(table_data, seat)
	tablelogic.leavetable(table_data, rid, seat.isrobot)	
	return responsemsg
end

function TableRequest.sitdowntable(rid, rdata, request)
 	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat ~= nil then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
		return responsemsg
	else
		if tablelogic.is_full(table_data) then
			if table_data.waits[rid] then
				tablelogic.leavetable(table_data, rid)
			end

			responsemsg.retcode = EErrCode.ERR_TABLE_FULL
			responsemsg.retstr = "当前桌子已经满了！"
			return responsemsg
		end

		seat = tablelogic.get_emptyseat_by_index(table_data)
		if seat == nil then
			if table_data.waits[rid] then
				tablelogic.leavetable(table_data, rid)
			end
			responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
			responsemsg.retstr = "当前桌子没有空座位了！"
			return responsemsg	
		end
	end
	tablelogic.sitdowntable(table_data, rid, rdata, request, seat)
    return responsemsg
end

function TableRequest.standuptable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if not seat then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "你已经站起了！"
		return responsemsg
	end
	tablelogic.standuptable(table_data, seat)
	return responsemsg
end

function TableRequest.shoot(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil or seat.launcher == nil then
		responsemsg.retcode = EErrCode.ERR_NOT_INTABLE
		responsemsg.retstr = "没有在座位上！"
		return responsemsg	
	end
	if seat.diamond < seat.launcher.Point * (seat.shootlevel or 1) then
		if seat.isrobot then
			tablelogic.passive_standuptable(table_data, seat)
		end

		responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
		responsemsg.retstr = "余额不足！"
		return responsemsg
	end
	gamelogic.shoot(rid, request.id, request.angle, seat.shootlevel, request.target_id)
	return responsemsg
end

function TableRequest.doaction(rid, rdata, request)
	return TableRequest.shoot(rid, rdata, request)
end

function TableRequest.collisiondetectioncheck(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_NOT_INTABLE
		responsemsg.retstr = "没有在座位上！"
		return responsemsg	
	end
	gamelogic.collisiondetectioncheck(request.bullet_id, request.target_id, request.target_ex)
	responsemsg.bullet_id = request.bullet_id
	responsemsg.target_id = request.target_id
	return responsemsg
end

function TableRequest.die(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	gamelogic.die(request.target_type, request.target_id)
	return responsemsg
end

function TableRequest.upgradelauncher(rid, rdata, request)
	if true then return end
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil or seat.launcher == nil then
		responsemsg.retcode = EErrCode.ERR_NOT_INTABLE
		responsemsg.retstr = "没有在座位上！"
		return responsemsg	
	end
	--[[
	local launcher = seat.launcher
	if request.level and request.level > 0 then
		if launcher.Level >= 5 then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "已经满级！"
			return responsemsg
		end
	else
		if launcher.Level <= 1 then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "已经最低级！"
			return responsemsg
		end
	end
	--]]
	tablelogic.upgradelauncher(table_data, seat, request)
	return responsemsg
end

function TableRequest.upgradeshoot(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
		responsemsg.retstr = "没有在座位上！"
		return responsemsg	
	end
	if not request.level or (request.level ~= 1 and request.level ~= -1) then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "参数错误！"
		return responsemsg
	end
	--[[
	if request.level == 1 then
		if seat.shootlevel >= table_data.conf.baseshootlevel + table_data.conf.upgradestage * (4 + table_data.conf.stageoffset) then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "参数错误！"
			return responsemsg
		end
	end
	if request.level == -1 then
		if seat.shootlevel <= table_data.conf.baseshootlevel then
			responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
			responsemsg.retstr = "参数错误！"
			return responsemsg
		end
	end
	--]]
	tablelogic.upgradeshoot(table_data, seat, request)
	return responsemsg
end

function TableRequest.useitem(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
		responsemsg.retstr = "没有在座位上！"
		return responsemsg
	end

	-- if request.itype == 1 then
	-- 	if seat.shootstat and seat.shootstat == 1 then
	-- 		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
	-- 		responsemsg.retstr = "锁定中，不能再使用道具！"
	-- 		return responsemsg
	-- 	end
	-- end
	
	local id, num = request.itype, request.num or 1
	if not id or id <= 0 or num <= 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "参数错误！"
		return responsemsg
	end

	-- if not seat.items or not seat.items[id] then
	-- 	responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
	-- 	responsemsg.retstr = "参数错误！没有道具！"
	-- 	return responsemsg
	-- end
	tablelogic.useitem(table_data, seat, seat.items[id], 1)
	return responsemsg
end

-- 修改捕鱼系数分布
function TableRequest.updatedistribution(request)
	gamelogic.updatedistribution(request.data)
end

-- 修改库存系数
function TableRequest.update_stock(request)
	if request.data < 0 then return end
	tablelogic.changestockratio(request.data)
end

return TableRequest